﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SharpPrivacy.SharpPrivacyLib.Cipher.Math;

namespace EncryptX
{ 
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        BigInteger[][] biKey;
        BigInteger[] biEncrypted;
        Byte[] bEncrypted;

        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //try
            {
                VeryLong l1 = new VeryLong(textBox1.Text);
                VeryLong l2 = new VeryLong(textBox2.Text);

                
                Int64 t1 = Int64.Parse(textBox1.Text);
                Int64 t2 = Int64.Parse(textBox2.Text);
                //VeryLong l;
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        textBox3.Text = (l1+l2).getInfo();
                        break;
                    case 1:
                        textBox3.Text = (l1 - l2).getInfo();
                        break;
                    case 2:
                        textBox3.Text = (l1 * l2).getInfo();
                        break;
                    case 3:
                        //textBox3.Text = (l1 / l2).getInfo();
                        //VeryLong q, r;
                        //q = VeryLong.vlongDivvlong(l1, l2, out r);
                        //textBox3.Text = (l1 / l2).getInfo();
                        textBox3.Text = String.Format("{0}\nCheck:\n {1}", (l1 / l2).getInfo(), (t1 / t2));
                        break;
                    case 4:
                        textBox3.Text = (l1 == l2).ToString();
                        break;
                    case 5:
                        textBox3.Text = (l1 < l2).ToString();
                        break;
                    case 6:
                        textBox3.Text = (l1 <= l2).ToString();
                        break;
                    case 7:
                        textBox3.Text = (l1 > l2).ToString();
                        break;
                    case 8:
                        textBox3.Text = (l1 >= l2).ToString();
                        break;
                    case 9:
                        int shift=int.Parse(textBox4.Text);
                        textBox3.Text = (VeryLong.compareTwo(l1,l2,shift)).ToString();
                        break;
                    case 10:
                        int ti = int.Parse(textBox2.Text);
                        textBox3.Text = (l1*ti).ToString();
                        break;
                    case 11:// %                        
                        textBox3.Text = String.Format("{0}\nCheck:\n {1}", (l1 % l2).getInfo(), (t1 % t2));
                        break;
                }
                //textBlock1.Text = l.getInfo();
            }
            //catch (Exception ex)
            //{ MessageBox.Show(String.Format("Error:\n{0}", ex)); }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            biKey = RSA.Generate(1024);
            RSA.RSA_Public_Key  biPk=RSA.extractPublicKey(biKey);

            Byte[] bInput = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
            BigInteger biInput=new BigInteger(bInput);

            biEncrypted=RSA.Encrypt(biInput, biPk);
            bEncrypted=biEncrypted[0].getBytes();

            textBox2.Text=biEncrypted[0].ToString();
            //textBox2.Text = BitConverter.ToString(bEncrypted);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            RSA.RSA_Secret_Key sk = RSA.extractSecretKey(biKey);

            BigInteger biDecrypted = RSA.Decrypt(biEncrypted, sk);
            Byte[] bDecrypted = biDecrypted.getBytes();

            //textBox4.Text = biDecrypted.ToString();
            //textBox4.Text = BitConverter.ToString(bDecrypted);
            textBox4.Text = System.Text.Encoding.UTF8.GetString(bDecrypted);
        }
    }
}
